#!/usr/bin/env python
# -*- coding: utf-8 -*-
from apps.models.dic_small_class_model import DictSmallClass

class DictSmallClassRepository:
    """
    @Author: kindey
    @Date: 2025/8/7
    @Description:
    """
    @staticmethod
    def get_device_info(device_id):
        try:
            return DictSmallClass.objects.get(id=device_id)
        except DictSmallClass.DoesNotExist:
            return None

    @staticmethod
    def get_devices_with_region_info():
        """
        获取设备及其区域信息的统计
        """
        return DictSmallClass.objects.extra(
            select={
                'reg_name': 'c.dict_name',
                'dict_name': 'd.dict_name',
                'records': 'COUNT(data.id)',
                'start_time': 'MIN(data.data_time)',
                'end_time': 'MAX(data.data_time)',
                'cid': 'c.id',
                'bid': 'b.id'
            },
            tables=['data', 'DictSmallClass b', 'DictSmallClass c', 'DictSmallClass d'],
            where=[
                'data.device_id = b.id',
                'c.id = b.field1',
                'd.dict_code = b.field2'
            ],
            group_by=['c.dict_name', 'd.dict_name', 'b.dict_name', 'b.class_name', 'c.id', 'b.id']
        )